package com.demo.datastructure;

import java.util.HashMap;

/**
 * @description:
 * @author: ljr
 * @time: 2021/5/16 11:20
 */
public class Solution {

    public int[] twoSum(int[] nums, int target) {
        if (nums.length == 0 || nums.length == 1) {
            return null;
        }

        if (nums[0] + nums[1] == target) {
            return new int[]{0, 1};
        }

        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < nums.length; i++){
            Integer index = map.get(nums[i]);
            if(index != null){
                return new int[]{index,i};
            }
            map.put(target-nums[i],i);
        }
        return null;
    }
}
